package com.yu.subject.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yu.subject.common.enums.CategoryTypeEnum;
import com.yu.subject.common.enums.IsDeletedFlagEnum;
import com.yu.subject.common.exception.CodeAndMessage;
import com.yu.subject.common.exception.JavaSmException;
import com.yu.subject.common.util.BeanCopyUtil;
import com.yu.subject.domain.entity.SubjectLabelBO;
import com.yu.subject.domain.service.SubjectLabelDomainService;
import com.yu.subject.infra.basic.entity.SubjectLabel;
import com.yu.subject.infra.basic.entity.SubjectMapping;
import com.yu.subject.infra.basic.service.SubjectCategoryService;
import com.yu.subject.infra.basic.service.SubjectLabelService;
import com.yu.subject.infra.basic.service.SubjectMappingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 路亚
 * @version 0.1
 * @className SubjectLabelDomainServiceImpl
 * @description
 * @date 2024/12/24 19:24
 * @since jdk11
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    private final SubjectLabelService subjectLabelService;

    private final SubjectMappingService subjectMappingService;

    private final SubjectCategoryService subjectCategoryService;

    @Override
    public void add(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = new SubjectLabel();
        BeanUtils.copyProperties(subjectLabelBO, subjectLabel);
        subjectLabel.setCreatedTime(new Date());
        subjectLabel.setUpdateTime(new Date());
        subjectLabelService.save(subjectLabel);
    }

    @Override
    public void updateById(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = new SubjectLabel();
        BeanUtils.copyProperties(subjectLabelBO, subjectLabel);
        subjectLabel.setUpdateTime(new Date());
        subjectLabelService.updateById(subjectLabel);
    }

    @Override
    public void deleteById(Long id) {
        subjectLabelService.removeById(id);
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(Long categoryId) {
        // 如果当前分类是1级分类，则查询当前分类下的所有标签
        if (CategoryTypeEnum.PRIMARY.getCode() == categoryId){
            QueryWrapper<SubjectLabel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category_id", categoryId);
            List<SubjectLabel> list = subjectLabelService.list(queryWrapper);
            List<SubjectLabelBO> result = BeanCopyUtil.copyListProperties(list, SubjectLabelBO.class);
            return result;
        }
        QueryWrapper<SubjectMapping> queryWrapper = new QueryWrapper();
        SubjectMapping subjectMapping = new SubjectMapping();
        queryWrapper.eq("category_id", categoryId);
        queryWrapper.eq("is_deleted", IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectMapping> list = subjectMappingService.list(queryWrapper);
        List<Long> collect = list.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)){
            throw new JavaSmException(CodeAndMessage.CATEGORY_NOT_LABEL);
        }
        List<SubjectLabel> subjectLabelList = subjectLabelService.listByIds(collect);
        List<SubjectLabelBO> result = BeanCopyUtil.copyListProperties(subjectLabelList, SubjectLabelBO.class);
        return result;
    }
}
